﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.DataAccess.Entities;
using RMS.Utils.MVP;
using RMS.Web.Shell.Presenter;
using RMS.Web.Shell.View;
using RMS.Utils.Navigator;

namespace RMS
{
    public partial class WriteReportView : Page<WriteReportPresenter>, IWriteReportView
    {
        RadiologyReport radiologyReport;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    GetRequest(Convert.ToInt32(Request.QueryString["RID"].ToString().Decrypt(Constants.ApplicationKey)));
                }
                catch (Exception)
                { }
            }
        }

        protected void gvRequests_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = (GridView)sender;
            int RequestID = Convert.ToInt32(gv.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            ButtonAction(e.CommandName, RequestID);
        }
        private void ButtonAction(string command, int RequestID)
        {
            if (command == "GetRequest")
            {
                GetRequest(RequestID);
            }
        }
        private void GetRequest(int RequestID)
        {
            try
            {
                var result = this.Presenter.CurrentRequest(RequestID);
                TXTPatientid.Text = result.PatientID.ToString();
                TXTPatientname.Text = result.FirstName + " " + result.FamilyName;
                TXTRequestid.Text = result.RequestId.ToString();
                btnGetDetails.Visible = true;
                BtnRetrieveRequest.Visible = false;
                EditorReport.Visible = true;
                BtnSave.Visible = true;
                Fillrepeter();
            }
            catch (Exception)
            {
                gvRequests.Visible = true;
            }
        }
        private void FillgvRequestsByPId(int PetientId)
        {
            var result = this.Presenter.GetRequest(PetientId);
            gvRequests.DataSource = result.Rows;
            gvRequests.DataBind();
        }
        private void FillgvRequestsByPName(String PetientName)
        {
            var result = this.Presenter.GetRequest(PetientName);
            gvRequests.DataSource = result.Rows;
            gvRequests.DataBind();
        }
        protected void BtnRetrieveRequest_Click(object sender, EventArgs e)
        {
            if (TXTRequestid.Text != "")
                GetRequest(Convert.ToInt32(TXTRequestid.Text));
            else if (TXTPatientname.Text != "")
                FillgvRequestsByPName(TXTPatientname.Text);
            else if (TXTPatientid.Text != "")
                FillgvRequestsByPId(Convert.ToInt32(TXTPatientid.Text));

        }
        private void Fillrepeter()
        {
            rptImag.DataSource = this.Presenter.GetImages(Convert.ToInt32(TXTRequestid.Text));
            rptImag.DataBind();
            rptImag.Visible = true;
        }
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            radiologyReport = new RadiologyReport();
            radiologyReport.report = EditorReport.Content;
            radiologyReport.RequestID = Convert.ToInt32(TXTRequestid.Text);
            this.Presenter.InsertReport(radiologyReport);
            Notification.Show();
        }

        protected void rptImag_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string path = e.Item.DataItem.ToString();
            HyperLink hplImage = (HyperLink)e.Item.FindControl("hplImage");
            HyperLink hplData = (HyperLink)e.Item.FindControl("hplData");
            hplImage.NavigateUrl = path + ".jpg";
            hplData.NavigateUrl = path + ".Xml";
        }

        protected void btnGetDetails_Click(object sender, EventArgs e)
        {
            Navigator.NavigateTo(Pages.WebPagesTechnical_RequestDetailsView, new string[] { "RID=" + TXTRequestid.Text });
        }
    }
}